<template>
  <BasicModal
    v-bind="$attrs"
    :canResize="false"
    @register="registerModal"
    :minHeight="100"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  // import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { accountFormSchema } from './group.data';
  // import { postUsers, putUsers } from '/@/api/sys/user';
  // const { createMessage } = useMessage();
  import { addGroup, addLabel, editGroup, editLabel } from '/@/api/sys/user';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'AccountModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
        labelWidth: 100,
        schemas: accountFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });
      // let oldUserName: string;
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        console.log(data);
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          // oldUserName = data.record.username;
          console.log(data.record);
          setFieldsValue({
            ...data.record,
            // enable: data.record.enable ? true : false,
          });
        }

        // if (!unref(isUpdate)) {
        //   appendSchemaByField(
        //     {
        //       field: 'enterpwd',
        //       label: '确认密码',
        //       component: 'InputPassword',
        //       dynamicRules: validatePass2,
        //     },
        //     'password',
        //   );
        // } else {
        //   removeSchemaByFiled('enterpwd');
        // }
        // updateSchema([
        //   {
        //     field: 'enable',
        //     show: unref(isUpdate),
        //   },
        // ]);

        // const treeData = await getDeptList();
        // updateSchema([
        //   {
        //     field: 'pwd',
        //     show: !unref(isUpdate),
        //   },
        //   {
        //     field: 'dept',
        //     componentProps: { treeData },
        //   },
        // ]);
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '添加标签' : '编辑标签'));
      const { createMessage } = useMessage();
      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });
          // TODO custom api
          console.log(values);
          if (unref(isUpdate)) {
            const nId = values.id;
            delete values.id;
            editLabel(nId, {
              ...values,
            }).then(
              () => {
                closeModal();
                createMessage.success('编辑标签成功');
                emit('success');
              },
              () => {
                // 业务层不提示了
                // createMessage.error('添加标签失败');
              },
            );
          } else {
            addLabel({
              ...values,
            }).then(
              () => {
                closeModal();
                createMessage.success('添加标签成功');
                emit('success');
              },
              () => {
                // 业务层不提示了
                // createMessage.error('添加标签失败');
              },
            );
          }
        } catch (e) {
          console.log(e);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, getTitle, handleSubmit };
    },
  });
</script>
